﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Orders;
using LuxDiner.Logic.Orders;
namespace LuxDiner.Web.Management.Areas.Orders.Controllers
{
    [RouteArea("Orders")]
    [RoutePrefix("PaidOrder")]
    public class PaidOrderApiController : ApiController
    {
        [Route("SaveOrders")]
        [HttpPost]
        public JsonEntity<PaidOrderModel> SaveOrders(JObject request)
        {
            string errinfo = "对不起！单据保存失败，出错原因：";
            try
            {
                #region 收集参数
                var order = new PaidOrderModel() { Items = new PaidOrderDetailCollection() };
                var items = request.GetValue("orderItems");

                order.NoteDate = request.Value<DateTime>("orderDate");
                order.CustomerID = request.Value<int>("customerID");
                order.OperatorUserID = Web.Global.WebContext.CurrentUser.ID;
                order.Method = request.Value<int>("method");
                order.Description = request.Value<string>("description");
                foreach (var it in items)
                {
                    order.Items.Add(new PaidOrderDetailModel()
                    {
                        BankID = it.Value<int>("bankID"),
                        Money = it.Value<decimal>("money")
                    });
                }
                #endregion

                var ret = PaidOrderLogic.NewOrder(order);
                return new JsonEntity<PaidOrderModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<PaidOrderModel>(null, errinfo, false);
        }
        
        [Route("GetOrders")]
        [HttpPost]
        public JsonEntitySet<PaidOrderModel> GetOrders(JObject request)
        {
            string errinfo = "no data";
            try
            {
                #region 收集参数
                PaidOrderQuery sea = new PaidOrderQuery();
                sea.PageSize = request.Value<int>("pageSize");
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.GetCustomer = request.Value<bool>("getCustomer");
                sea.CustomerID = request.Value<int>("customerID");
                sea.OrderNo = request.Value<string>("orderNo");
                sea.GetItems = request.Value<bool>("getItems");
                sea.Method = request.Value<int>("method");

                var odb = request.Value<string>("orderDateBefore");
                if (!string.IsNullOrEmpty(odb))
                    sea.OrderDateBefore = DateTime.Parse(odb);
                var oda = request.Value<string>("orderDateAfter");
                if (!string.IsNullOrEmpty(oda))
                    sea.OrderDateAfter = DateTime.Parse(oda);
                #endregion

                var ret = PaidOrderLogic.GetOrders(sea);
                return new JsonEntitySet<PaidOrderModel>(ret);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<PaidOrderModel>(null, errinfo, false);
        }
    }
}
